package com.xue.geo.area;

import com.xue.domain.AreaInfo;
import com.xue.geo.domain.Coordinate;
import com.xue.utils.LatLonUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;

/**
 * @author xue
 * @create 2022-09-16 17:28
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class SquareAreaJudge extends AreaJudge {

    private Integer latMax;

    private Integer latMin;

    private Integer lonMax;

    private Integer lonMin;


    @Override
    public AreaJudge createArea(AreaInfo areaBase) {
        Integer lat = areaBase.getLat();
        Integer lon = areaBase.getLon();
        Integer ldm = areaBase.getLdm();
        Integer sdm = areaBase.getSdm();
        Integer ldmVariable = BigDecimal.valueOf(ldm).divide(BigDecimal.valueOf(1000), 3, BigDecimal.ROUND_HALF_UP).multiply(PROPORTION).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
        Integer sdmVariable = BigDecimal.valueOf(sdm).divide(BigDecimal.valueOf(1000), 3, BigDecimal.ROUND_HALF_UP).multiply(PROPORTION).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();

        latMax = lat + ldmVariable;
        latMin = lat - ldmVariable;

        lonMax = lon + sdmVariable;
        lonMin = lon - sdmVariable;
        return this;
    }

    @Override
    public Boolean pointInside(Coordinate coordinate) {
        Integer longitude = LatLonUtil.translationPoint(coordinate.getLongitude());
        Integer latitude = LatLonUtil.translationPoint(coordinate.getLatitude());
        if (latitude > latMax || latitude < latMin || longitude > lonMax || longitude < lonMin) {
            return false;
        }
        return true;
    }
}
